package com.sinodata.bsm.cicp.finder.aix;

/**
 * AIX CPU发现程序.
 * 程序返回：CPU主频，CPU ID，CPU类型
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class Cpu extends AixFinder {
    private static final Logger logger = Logger.getLogger(Cpu.class);
    private static final String SHELL = "lsattr -El proc0";

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] cpuRes = null;
        BufferedReader reader = null;
        String strLine = null;
        String cpuTypeRegex = "type\\s+(\\S*)\\s+.*";
        String cpuFreqRegex = "frequency\\s+(\\d+)\\s+.*";
        String cpuType = "N/A";
        String cpuFre = "N/A";
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            Pattern pattern = null;
            Matcher matcher = null;
            while ((strLine = reader.readLine()) != null) {
                //发现CPU类型
                pattern = Pattern.compile(cpuTypeRegex);
                matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    cpuType = matcher.group(1);
                    continue;
                }
                //发现CPU主频
                pattern = Pattern.compile(cpuFreqRegex);
                matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    cpuFre = matcher.group(1);
                }
            }
            String cmdStr = "sar -u -P ALL 1 | awk '{if($1~/:/) {print $2,$3,$4,$5,$6,$7} else {print $0}}'|grep -v -";
            String regex = "\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+).*";
            reader = CommandReader.getReader(ip, cmdStr, 0, 0);
            String str = null;
            while ((str = reader.readLine()) != null) {
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(str);
                if (m.find()) {
                    cpuRes = new String[6];
                    list.add(cpuRes);
                    cpuRes[0] = m.group(1);
                    cpuRes[1] = "CPU" + m.group(1);
                    cpuRes[2] = "CPU" + getHostName(ip) + m.group(1);
                    cpuRes[3] = cpuFre; //CPU主频
                    cpuRes[4] = m.group(1); //CPU ID
                    cpuRes[5] = cpuType; //CPU类型
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find aix cpu info", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
